package com.leyou.web;


import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.service.SkuService;
import com.leyou.service.SpuDetailService;
import com.leyou.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.leyou.dto.PageDTO;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuDetailService spuDetailService;

    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> pageQuery(
            @RequestParam(value = "brandId",required = false)Integer brandId,
            @RequestParam(value = "categoryId",required = false)Integer categoryId,
            @RequestParam(value ="id",required = false)Integer spuId,
            @RequestParam(value = "page",defaultValue = "1")Integer currentPage,
            @RequestParam(value = "rows",defaultValue = "5")Integer pageSize,
            @RequestParam(value ="saleable",required = false)Boolean  saleable

    ) {
        return ResponseEntity.ok(spuService.pageQuery(brandId,categoryId,spuId,currentPage,pageSize,saleable));
    }

    /**
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO) {

        spuService.addGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam(value = "id",required = false)Long id,@RequestParam(value = "saleable",required = false)Boolean saleable) {

        spuService.updateSaleable(id,saleable);

        return ResponseEntity.status(HttpStatus.OK).build();

    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> getGoodById(@PathVariable("id")Long id) {


        return ResponseEntity.ok( spuService.getGoodById(id));
    }

    /**
     * 更新商品
     * @param spuDTO 页面提交商品信息
     * @return 无
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询SpuDetail
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> getSpuDetailsById(@RequestParam("id")Long id) {

        return ResponseEntity.ok(spuDetailService.getSpuDetailsById(id));

    }

    /**
     * 根据spu的id查询sku的集合
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> getSkusBySpuId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(skuService.getSkusBySpuId(id));
    }

    /**
     * 根据skuids查询id的集合
     * @param skuIds
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> getSkusBySkuIds(@RequestParam("ids") List<Long> skuIds) {
        return ResponseEntity.ok(skuService.getSkusBySkuIds(skuIds));
    }

//    @GetMapping("/spec/value")
//////    public ResponseEntity<>

    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> getSpuBySpuId(@PathVariable("id") Long id) {
        return ResponseEntity.ok(spuService.getSpuBySpuId(id));
    }

    /**
     * 根据spuId查询spu的所有规格参数键值对
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> getAllSpecParamBySpuId(@RequestParam("id")Long id, @RequestParam(value = "searching", required = false)Boolean searching){

        return ResponseEntity.ok(spuService.getAllSpecParamBySpuId(id,searching));

    }







}
